﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;
using Ext.Net;

namespace BitPortal.Admin.Service.Errors.Module
{
    public partial class ErrorAdd : Controls.BaseAdminControl
    {
        ExtendedBitPortalEntities extPortal = new ExtendedBitPortalEntities();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            Models.ErrorReport error = new Models.ErrorReport()
            {
                
                ErrorName = txtName.Text,
                DesOfCustomer = txtDiscription.Text,
                DateReport = System.DateTime.Now,
                IsAttach = this.ckHaveAttachFile.Checked,
                ProductCustomerId = Convert.ToInt64(prouctCustomerID.Value),
                FilePath = this.thumImage.TextFieldURL.Text,
                IsDeleted=false,
                StateError=(int)StateError.Chuaduyet,
                IsApproved=false
            };

            #region clear 
            txtName.Clear();
            prouctCustomerID.Clear();
            txtDiscription.Clear();
            errorId.Clear();
            txtName.Clear();
            ckHaveAttachFile.Checked = false;
            thumImage.TextFieldURL.Clear();
             #endregion
            ProductCustomerBLL.AddCountError(error.ProductCustomerId);
            if (error.Insert() > 0)
            {
                (this.Page as Controls.BaseAdminPage).ShowNotification("Thêm lỗi thành công");
                Window1.Hide();
                ReloadTargetData();
            }
        }
     
        protected void updateBtn_Click(object sender, EventArgs e)
        {
            ExtendedBitPortalEntities ent = new ExtendedBitPortalEntities();
            long errorID=Convert.ToInt64(errorId.Text);
            ErrorReport error=ent.ErrorReports.SingleOrDefault(pp=>pp.ErrorID==errorID);
            //Models.ErrorReport error = new Models.ErrorReport()
            //{
             error.ErrorName =txtName.Text;
             error.DesOfCustomer = txtDiscription.Text;
             error.DateReport = System.DateTime.Now;
             error.IsAttach = this.ckHaveAttachFile.Checked;
             error.ProductCustomerId = Convert.ToInt64(prouctCustomerID.Text);
             error.FilePath = this.thumImage.TextFieldURL.Text;
             error.IsApproved=false;
             error.IsDeleted=false;
             error.StateError = (int)StateError.Chuaduyet;
            //};
           // error.ErrorID = Convert.ToInt64(errorId.Text);
            //int index = ErrorReportBLL.Update(error);
            int index=ent.SaveChanges();
            if (index>0)
            {
                (this.Page as Controls.BaseAdminPage).ShowNotification("Sửa lỗi thành công");
                Window1.Hide();
                ReloadTargetData();
            }
        }

        public void BindData(long errorID = 0)
        {
            if (errorID > 0)
            {
                BitPortal.Models.ErrorReport error = ErrorReportBLL.GetByErrorID(errorID);
                if (error != null)
                {
                    this.errorId.Text = error.ErrorID.ToString();
                    this.prouctCustomerID.Text = error.ProductCustomerId.ToString();
                    
                    this.txtName.Text = error.ErrorName.ToString();
                    this.txtDiscription.Text = error.DesOfCustomer.ToString();
                    this.ckHaveAttachFile.Checked = error.IsAttach;
                    this.thumImage.TextFieldURL.Text = error.FilePath;
                    this.btnAdd.Hidden = true;

                    Window1.Show();
                }
            }
        }

       
        public void CreateError(long productCustomerId)
        {
            if (productCustomerId>0)
            {
                this.prouctCustomerID.Text = productCustomerId.ToString();

                this.updateBtn.Hidden = true;
                
                Window1.Show();
            }
        }
    }
}